package com.nike.amoeba.viewmodel

import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.nike.amoeba.R
import com.nike.amoeba.base.MyBaseViewModel
import com.nike.amoeba.bean.DataBean
import com.nike.amoeba.ext.showMessage
import com.nike.amoeba.network.REQUEST_SUCCESS
import com.nike.amoeba.network.apiService
import com.nike.amoeba.network.stateCallback.ListDataUiState
import com.nike.amoeba.ui.frg.StockAdjListFrg
import me.hgj.jetpackmvvm.base.appContext
import me.hgj.jetpackmvvm.ext.requestNoCheck
import org.json.JSONArray
import org.json.JSONObject

/**
 * User: Nike
 *  2024/5/28 10:28
 */
class StockAdjustModel : MyBaseViewModel() {

    var listBean: MutableLiveData<ListDataUiState<DataBean>> = MutableLiveData()

    var adjId: String? = null

    fun onRequest(code: String?) {
        requestNoCheck({ apiService.getInventoryList(code) }, {
            val json = JSONObject(it.string())
            if (json.optInt("code") == REQUEST_SUCCESS){
                val data = json.optJSONArray("data")
                if (data != null) {
                    for (i in 0 until data.length()) {
                        val obj = data.optJSONObject(i)
                        val Attributes = obj.optJSONObject("Attributes")
                        val keys: Iterator<String> = Attributes.keys()
                        val listStr = ArrayList<DataBean>()
                        while (keys.hasNext()) {
                            val key = keys.next()
                            val value: String = Attributes.getString(key)
                            val bean = DataBean()
                            bean.Title = key
                            bean.content = value
                            listStr.add(bean)
                        }
                        obj.put("content", Gson().toJson(listStr).toString())
                    }
                    val listType = object : TypeToken<List<DataBean>>() {}.type
                    val list: ArrayList<DataBean> = Gson().fromJson(data.toString(), listType)
                    listBean.value = (
                        ListDataUiState(
                            isRefresh = true,
                            isSuccess = true,
                            listData = list as java.util.ArrayList<DataBean>,
                            isFirstEmpty = if (list!!.size == 0) true else false
                        ))
                }
            }
        }, {
            //请求失败 网络异常回调在这里
            loadingChange.dismissDialog
            ToastUtils.showShort(it.throwable!!.message)
            LogUtils.e(it.throwable)
        }, false)
    }

    fun onInventoryDetail(orderId: String?, code: String) {
        this.adjId = orderId
        requestNoCheck({ apiService.getInventoryDetail(code, orderId!!) }, {
            if (it.code == REQUEST_SUCCESS){
                val data = it.data
                listBean.value = (
                    ListDataUiState(
                        isRefresh = true,
                        isSuccess = true,
                        listData = data as java.util.ArrayList<DataBean>,
                        isFirstEmpty = if (data!!.size == 0) true else false
                    ))
            }
        }, {
            //请求失败 网络异常回调在这里
            loadingChange.dismissDialog
            ToastUtils.showShort(it.throwable!!.message)
            LogUtils.e(it.throwable)
        }, false)
    }

    fun onUpload(
        mFilterList: java.util.ArrayList<DataBean>,
        orderId: String?,
        frg: StockAdjListFrg,
        code: String
    ) {
        if (mFilterList.all { it.num == 0 })return
        val ja = JSONArray()
        mFilterList.forEach {
            var obj = JSONObject()
            obj.put("RoNo", it.RoNo)
            obj.put("BatchNo", it.BatchNo)
            obj.put("MURoNo", it.MURoNo)
            obj.put("UNRoNo_Last", it.UNRoNo_Last)
            obj.put("MUnitName", it.MUnitName)
            obj.put("SUnitName", it.SUnitName)
            obj.put("Inventory", it.Inventory)
            obj.put("num", it.num)
            obj.put("ARoNo", orderId)
            ja.put(obj)

            it.Inventory = it.num
        }
        requestNoCheck({ apiService.UpdateInventory(code, ja.toString(), orderId) }, {
            if (it.code == REQUEST_SUCCESS){
                /*listBean.value = (
                    ListDataUiState(
                        isRefresh = true,
                        isSuccess = true,
                        listData = mFilterList as java.util.ArrayList<DataBean>,
                        isFirstEmpty = if (mFilterList!!.size == 0) true else false
                    ))*/
                onInventoryDetail(this.adjId, code)
                frg.showMessage(message = frg.getString(R.string.text3))
            }
        }, {
            //请求失败 网络异常回调在这里
            loadingChange.dismissDialog
            ToastUtils.showShort(it.throwable!!.message)
            LogUtils.e(it.throwable)
        }, true, appContext.getString(R.string.loading))

    }

}